package com.lry.basic.algorithm.common;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author:刘仁有
 * @desc:
 * @email:953506233@qq.com
 * @data:2019/9/7
 */
public class Graph extends JFrame{

    public void launchframe() {
        setSize(350,400);
        setLocation(100,50);
        setVisible(true);
    }
    @Override
    public void paint(Graphics g) {
        try {
            BufferedImage bufImg = createImg();
            g.drawImage(bufImg,0,100,null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        Graph gf = new Graph();
        gf.launchframe();
    }
    public static void print(int[][] img){
        for(int i=0;i<50;i++){
            for(int j=0;j<10;j++){
                System.out.print(img[i][j]+" ");
            }
            System.out.println();
        }
    }
    public BufferedImage createImg() throws IOException {
        int[][]matrix = getImageRGB("d:\\1.png");
        int width = matrix.length;
        int height = matrix[0].length;
        BufferedImage bi = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphic = bi.createGraphics();
        graphic.setColor(new Color(255,255,255));
        graphic.fillRect(0, 0, width, height);
        int[][] newMatrix = rotateMN(matrix);
        for (int i = 0; i < newMatrix.length; i++) {
                for (int j = 0; j < newMatrix[0].length; j++) {
                    bi.setRGB(i, j, newMatrix[i][j]);
                }
        }
        return bi;
    }


    public static int[][] getImageRGB(String filePath) {
        File file  = new File(filePath);
        int[][] result = null;
        if (!file.exists()) {
            return result;
        }
        try {
            BufferedImage bufImg = ImageIO.read(file);
            int height = bufImg.getHeight();
            int width = bufImg.getWidth();
            result = new int[width][height];
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    result[i][j] = bufImg.getRGB(i, j) & 0xFFFFFF;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
    // n*n的矩阵向右旋转90°
    public static int[][] rotateMN(int[][] matrix){
        int m=matrix.length;//行
        int n=matrix[0].length;//列
        //新矩阵
        int [][] newMatrix = new int[n][m];
        //先转置
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                newMatrix[j][i] = matrix[i][j];
            }
        }
        //再reverse每一行
        for(int i=0;i<n;i++){
            for(int j=0;j<m/2;j++){
                int temp = newMatrix[i][j];
                newMatrix[i][j] = newMatrix[i][m-1-j];
                newMatrix[i][m-1-j] = temp;
            }
        }
        return newMatrix;
    }
}
